﻿using ET_DbCommon.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET_DbCommon.DAL
{
    public static class Exam_TestQuestion_Task_StepDAL
    {
        /// <summary>
        /// 创建考核关联的场景任务
        /// </summary>
        /// <param name="MyExam_TestQuestionId">场景索引</param>
        public static Task InsertToExam_TestQuestion_Task_Step(Exam_TestQuestion_TaskModel MyExam_TestQuestion_TaskModel, int MyPaper_TestQuestion_TaskId)
        {
            try
            {
                using (var conn = MySqlHelper.GetConnection())
                {
                    var MyPaper_TestQuestion_Task_StepModels = Paper_TestQuestion_Task_StepDAL.GetMyPaper_TestQuestion_Task_StepModel(MyPaper_TestQuestion_TaskId);
                    if (MyPaper_TestQuestion_Task_StepModels != null)
                    {
                        foreach (var MyPaper_TestQuestion_Task_StepModel in MyPaper_TestQuestion_Task_StepModels)
                        {
                            Exam_TestQuestion_Task_StepModel exam_TestQuestion_Task_StepModel = new Exam_TestQuestion_Task_StepModel();
                            exam_TestQuestion_Task_StepModel.TaskId = MyPaper_TestQuestion_TaskId;
                            exam_TestQuestion_Task_StepModel.ExamTaskId = MyExam_TestQuestion_TaskModel.Id;
                            exam_TestQuestion_Task_StepModel.OperateId = MyPaper_TestQuestion_Task_StepModel.OperateId;
                            exam_TestQuestion_Task_StepModel.Parameter = MyPaper_TestQuestion_Task_StepModel.Parameter;
                            exam_TestQuestion_Task_StepModel.Score = MyPaper_TestQuestion_Task_StepModel.Score;
                            exam_TestQuestion_Task_StepModel.SysType = MyPaper_TestQuestion_Task_StepModel.SysType;
                            exam_TestQuestion_Task_StepModel.TaskType = MyPaper_TestQuestion_Task_StepModel.TaskType;
                            exam_TestQuestion_Task_StepModel.Describes = MyPaper_TestQuestion_Task_StepModel.Describes;
                            exam_TestQuestion_Task_StepModel.DevicetypeId = MyPaper_TestQuestion_Task_StepModel.DevicetypeId;
                            exam_TestQuestion_Task_StepModel.DeviceId = MyPaper_TestQuestion_Task_StepModel.DeviceId;
                            exam_TestQuestion_Task_StepModel.StepContent = MyPaper_TestQuestion_Task_StepModel.StepContent;
                            exam_TestQuestion_Task_StepModel.Prerequisite = MyPaper_TestQuestion_Task_StepModel.Prerequisite;
                            exam_TestQuestion_Task_StepModel.KeyWords = MyPaper_TestQuestion_Task_StepModel.KeyWords;
                            exam_TestQuestion_Task_StepModel.TimeOut = MyPaper_TestQuestion_Task_StepModel.TimeOut;
                            exam_TestQuestion_Task_StepModel.Duration = MyPaper_TestQuestion_Task_StepModel.Duration;
                            exam_TestQuestion_Task_StepModel.IsContinued = MyPaper_TestQuestion_Task_StepModel.IsContinued;
                            exam_TestQuestion_Task_StepModel.RoleId = MyPaper_TestQuestion_Task_StepModel.RoleId;
                            exam_TestQuestion_Task_StepModel.UIOrder = MyPaper_TestQuestion_Task_StepModel.UIOrder;
                            exam_TestQuestion_Task_StepModel.PaperStepId = MyPaper_TestQuestion_Task_StepModel.Id;
                            exam_TestQuestion_Task_StepModel.ExcuteState = "未执行";
                            exam_TestQuestion_Task_StepModel.Result =0;
                            var lastId = MySQLExt.InsertIdentityIndex<Exam_TestQuestion_Task_StepModel>(conn, exam_TestQuestion_Task_StepModel);
                            exam_TestQuestion_Task_StepModel.Id = lastId;
                            if (MyExam_TestQuestion_TaskModel.Exam_Steps == null)
                                MyExam_TestQuestion_TaskModel.Exam_Steps = new List<Exam_TestQuestion_Task_StepModel>();
                            if (!MyExam_TestQuestion_TaskModel.Exam_Steps.Any(model => model.Equals(exam_TestQuestion_Task_StepModel)))
                            {
                                exam_TestQuestion_Task_StepModel.OperateContent = OperateItemModel.InStance().GetOperateItem(MyPaper_TestQuestion_Task_StepModel.OperateId).Describes;
                                MyExam_TestQuestion_TaskModel.Exam_Steps.Add(exam_TestQuestion_Task_StepModel);
                            }
                        }
                    }
                }
                return Task.CompletedTask;
            }
            catch(Exception ex)
            {
                return null;
            }
        }
    }
}
